package com.eco.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eco.annotation.AuthToken;
import com.eco.dao.ds3.FishTypeMapper;
import com.eco.entity.AiFishEntity;
import com.eco.entity.AiFishTypeEntity;
import com.eco.entity.FishType;
import com.eco.service.AiFishService;
import com.eco.service.AiFishService2;
import com.eco.utils.DateAndStringUtils;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.tags.BindTag;

@RestController
/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/controller/AiFishController.class */
public class AiFishController {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) AiFishController.class);

    @Resource
    private AiFishService aiFishServiceIn;

    @Resource
    private AiFishService2 aiFishServiceOut;

    @Value("${station.ai.preAiUrlIn}")
    private String preAiUrlIn;

    @Value("${station.ai.preAiUrlOut}")
    private String preAiUrlOut;

    @Value("${station.ai.preFishPicture}")
    private String preFishPicture;

    @Autowired
    private FishTypeMapper fishTypeMapper;

    
    @GetMapping({"/getAiFishTable"})
    public JSONObject getAiFishTable(String startDate, String endDate, String pageNum, String point, String direction, String category, String length) {
        List<AiFishEntity> aiFishList;
        int total;
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate) || StringUtils.isEmpty(point)) {
            log.error("有日期参数为空");
            result.put("msg", "有日期参数为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
        }
        Float lenMin = null;
        Float lenMax = null;
        if (!StringUtils.isEmpty(length) && StringUtils.contains(length, "-")) {
            String[] split = StringUtils.split(length.trim(), "-");
            if (StringUtils.endsWith(length, "-")) {
                lenMin = Float.valueOf(split[0]);
            }
            if (split.length == 2) {
                lenMin = Float.valueOf(split[0]);
                lenMax = Float.valueOf(split[1]);
            }
        }
        JSONArray datas = new JSONArray();
        if (StringUtils.equals("in", point)) {
            aiFishList = this.aiFishServiceIn.findByDate(startDate, endDate, Integer.parseInt(pageNum.trim()), 10, direction, category, lenMin, lenMax);
            total = this.aiFishServiceIn.countByRecordDate(startDate, endDate, direction, category, lenMin, lenMax);
        } else {
            aiFishList = this.aiFishServiceOut.findByDate2(startDate, endDate, Integer.parseInt(pageNum.trim()), 10, direction, category, lenMin, lenMax);
            total = this.aiFishServiceOut.countByRecordDate2(startDate, endDate, direction, category, lenMin, lenMax);
        }
        if (CollectionUtils.isEmpty(aiFishList)) {
            log.error("未查询到过鱼数据");
            result.put("msg", "未查询到过鱼数据");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        aiFishList.stream().forEach(aiFishEntity -> {
            JSONObject data = new JSONObject();
            data.put("recordId", (Object) aiFishEntity.getId());
            data.put("recordTime", (Object) aiFishEntity.getCreatedAt());
            data.put("width", (Object) aiFishEntity.getWidth());
            data.put("length", (Object) aiFishEntity.getLength());
            data.put("category",aiFishEntity.getFishType());
            data.put("categoryName",aiFishEntity.getAiFishTypeEntity().getFishName());
            data.put("weight", (Object) aiFishEntity.getWeight());
            data.put("direction", (Object) aiFishEntity.getSwim());
            if (StringUtils.equals("in", point)) {
                data.put("imgUrl", (Object) (this.preAiUrlIn + DateAndStringUtils.getSuffixUrl(aiFishEntity.getId(), aiFishEntity.getCreatedAt(), aiFishEntity.getTimes(), aiFishEntity.getTimestamp()) + ".jpg"));
                data.put("videoUrl", (Object) (this.preAiUrlIn + DateAndStringUtils.getSuffixUrl(aiFishEntity.getId(), aiFishEntity.getCreatedAt(), aiFishEntity.getTimes(), aiFishEntity.getTimestamp()) + ".mp4"));
            } else {
                data.put("imgUrl", (Object) (this.preAiUrlOut + DateAndStringUtils.getSuffixUrl(aiFishEntity.getId(), aiFishEntity.getCreatedAt(), aiFishEntity.getTimes(), aiFishEntity.getTimestamp()) + ".jpg"));
                data.put("videoUrl", (Object) (this.preAiUrlOut + DateAndStringUtils.getSuffixUrl(aiFishEntity.getId(), aiFishEntity.getCreatedAt(), aiFishEntity.getTimes(), aiFishEntity.getTimestamp()) + ".mp4"));
            }
            data.put("imgUrl2", data.get("imgUrl"));
            datas.add(data);
        });
        log.info("查询过鱼识别数据(table)成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("data", (Object) datas);
        result.put("total", Integer.valueOf(total));
        return result;
    }

    
    @GetMapping({"/getAiOrSonarFishChart"})
    public JSONObject getSonarFishChart(String startDate, String endDate, String point, String mode) {
        List<AiFishEntity> aiFishEntityList;
        int up;
        int down;
        int upTotal;
        int downTotal;
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            log.error("有日期参数为空");
            result.put("msg", "有日期参数为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        if (StringUtils.isEmpty(point)) {
            point = "in";
        }
        if (StringUtils.isEmpty(mode)) {
        }
        JSONArray datas = new JSONArray();
        String dateStr = DateAndStringUtils.dateNow();
        if (StringUtils.equals("in", point)) {
            aiFishEntityList = this.aiFishServiceIn.findByRecordDateTime(startDate, endDate);
            List<AiFishEntity> todayFishs = this.aiFishServiceIn.findByRecordDateTime(dateStr + " 00:00:00", dateStr + " 23:59:59");
            AtomicInteger upNum = new AtomicInteger();
            AtomicInteger downNum = new AtomicInteger();
            if (CollectionUtils.isEmpty(todayFishs)) {
                upNum.set(0);
                downNum.set(0);
            } else {
                todayFishs.stream().forEach(aiFishEntity -> {
                    if ("0".equals(aiFishEntity.getSwim())) {
                        upNum.addAndGet(1);
                    } else {
                        downNum.addAndGet(1);
                    }
                });
            }
            up = upNum.intValue();
            down = downNum.intValue();
            upTotal = this.aiFishServiceIn.countAll("right");
            downTotal = this.aiFishServiceIn.countAll("left");
        } else {
            aiFishEntityList = this.aiFishServiceOut.findByRecordDateTime2(startDate, endDate);
            List<AiFishEntity> todayFishs2 = this.aiFishServiceOut.findByRecordDateTime2(dateStr + " 00:00:00", dateStr + " 23:59:59");
            AtomicInteger upNum2 = new AtomicInteger();
            AtomicInteger downNum2 = new AtomicInteger();
            if (CollectionUtils.isEmpty(todayFishs2)) {
                upNum2.set(0);
                downNum2.set(0);
            } else {
                todayFishs2.stream().forEach(aiFishEntity2 -> {
                    if ("0".equals(aiFishEntity2.getSwim())) {
                        upNum2.addAndGet(1);
                    } else {
                        downNum2.addAndGet(1);
                    }
                });
            }
            up = upNum2.intValue();
            down = downNum2.intValue();
            upTotal = this.aiFishServiceOut.countAll2("right");
            downTotal = this.aiFishServiceOut.countAll2("left");
        }
        if (CollectionUtils.isEmpty(aiFishEntityList)) {
            log.error("未查询到过鱼识别数据");
            result.put("msg", "未查询到过鱼识别数据");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            result.put("todayUp", Integer.valueOf(up));
            result.put("todayDown", Integer.valueOf(down));
            result.put("todayTotal", Integer.valueOf(up + down));
            result.put("totalUp", Integer.valueOf(upTotal));
            result.put("totalDown", Integer.valueOf(downTotal));
            result.put("totalTotal", Integer.valueOf(upTotal + downTotal));
            return result;
        }
        aiFishEntityList.stream().forEach(aiFishEntity3 -> {
            JSONObject data = new JSONObject();
            data.put("recordTime", (Object) aiFishEntity3.getCreatedAt());
            data.put("width", (Object) aiFishEntity3.getWidth());
            data.put("length", (Object) aiFishEntity3.getLength());
            data.put("weight", (Object) aiFishEntity3.getWeight());
            data.put("direction", (Object) aiFishEntity3.getSwim());
            data.put("category",aiFishEntity3.getAiFishTypeEntity().getFishName());
            datas.add(data);
        });
        log.info("查询识别过鱼数据(Chart)成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("data", (Object) datas);
        result.put("todayUp", Integer.valueOf(up));
        result.put("todayDown", Integer.valueOf(down));
        result.put("todayTotal", Integer.valueOf(up + down));
        result.put("totalUp", Integer.valueOf(upTotal));
        result.put("totalDown", Integer.valueOf(downTotal));
        result.put("totalTotal", Integer.valueOf(upTotal + downTotal));
        return result;
    }

    @PostMapping(value = {"/updateAiFish"}, consumes = {"application/json"})
    public JSONObject updateFishData(@RequestBody JSONObject data) {
        JSONObject result = new JSONObject();
        String point = data.getString("point");
        String id = data.getString("id");
        data.getString("fishType");
        if (StringUtils.isEmpty(point) || StringUtils.isEmpty(id)) {
            log.error("fishType、id或point为空");
            result.put("msg", (Object) "fishType、id或point为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        Integer idi = data.getInteger("id");
        AiFishEntity aiFishEntity = (AiFishEntity) data.toJavaObject(AiFishEntity.class);
        if (StringUtils.equals("in", point)) {
            AiFishEntity byId = this.aiFishServiceIn.findById(idi.intValue());
            if (byId == null) {
                log.error("没有查询到要修改的数据");
                result.put("msg", (Object) "没有查询到要修改的数据");
                result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
                result.put("data", (Object) null);
                return result;
            }
            if (aiFishEntity == null) {
                log.error("JSONObject转AiFishEntity异常");
                result.put("msg", (Object) "数据转换异常");
                result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
                result.put("data", (Object) null);
                return result;
            }
            aiFishEntity.setUpdatedAt(DateAndStringUtils.dateTimeNow());
            aiFishEntity.setFishType(byId.getFishType());
            int isok = this.aiFishServiceIn.updateByAiFishEntity(aiFishEntity);
            if (isok < 1) {
                log.error("修改进口识别数据[{}]失败，数据库操作异常", id);
                result.put("msg", (Object) "操作数据库异常");
                result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
                result.put("data", (Object) null);
                return result;
            }
            log.info("修改进口识别数据[{}]成功", id);
            result.put("msg", (Object) "修改成功");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
            result.put("data", (Object) null);
            return result;
        }
        AiFishEntity byId2 = this.aiFishServiceOut.findById2(idi.intValue());
        if (byId2 == null) {
            log.error("没有查询到要修改的数据");
            result.put("msg", (Object) "没有查询到要修改的数据");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        if (aiFishEntity == null) {
            log.error("JSONObject转RecordDTO异常");
            result.put("msg", (Object) "数据转换异常");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        aiFishEntity.setUpdatedAt(DateAndStringUtils.dateTimeNow());
        aiFishEntity.setFishType(byId2.getFishType());
        int isok2 = this.aiFishServiceOut.updateByAiFishEntity2(aiFishEntity);
        if (isok2 < 1) {
            log.error("修改出口识别数据[{}]失败，数据库操作异常", id);
            result.put("msg", (Object) "操作数据库异常");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        log.info("修改出口识别数据[{}]成功", id);
        result.put("msg", (Object) "修改成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("data", (Object) null);
        return result;
    }

    @RequestMapping({"/deleteAiFish"})
    
    public JSONObject deleteAiFish(int[] ids, String point) {
        JSONObject result = new JSONObject();
        if (ids.length == 0 || StringUtils.isEmpty(point)) {
            log.error("id数组或点位为空");
            result.put("msg", (Object) "id数组或点位为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        if (StringUtils.equals("in", point)) {
            this.aiFishServiceIn.deleteById(ids);
            log.info("删除进口过鱼识别数据{}成功", ids);
            result.put("msg", (Object) "删除成功");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
            result.put("data", (Object) null);
            return result;
        }
        this.aiFishServiceOut.deleteById2(ids);
        log.info("删除出口过鱼识别数据{}成功", ids);
        result.put("msg", (Object) "删除成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("data", (Object) null);
        return result;
    }

    
    @GetMapping({"/getAllFishName"})
    public JSONObject getAllFishName(String point) {
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(point)) {
            log.error("区分点位的参数为空");
            result.put("msg", "dataType为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        if (StringUtils.equals("in", point)) {
            List<AiFishTypeEntity> allFishType = this.aiFishServiceIn.findAllFishType();
            if (CollectionUtils.isEmpty(allFishType)) {
                log.error("进口没有种类数据");
                result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
                result.put("data", (Object) null);
                return result;
            }
            JSONArray datas = new JSONArray();
            allFishType.stream().forEach(aiFishTypeEntity -> {
                JSONObject data = new JSONObject();
                data.put("fishName", (Object) aiFishTypeEntity.getFishName());
                data.put("fishType", (Object) aiFishTypeEntity.getObject());
                datas.add(data);
            });
            log.info("查询进口种类成功");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
            result.put("data", (Object) datas);
            return result;
        }
        List<AiFishTypeEntity> allFishType2 = this.aiFishServiceOut.findAllFishType2();
        if (CollectionUtils.isEmpty(allFishType2)) {
            log.error("出口没有种类数据");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            result.put("data", (Object) null);
            return result;
        }
        JSONArray datas2 = new JSONArray();
        allFishType2.stream().forEach(aiFishTypeEntity2 -> {
            JSONObject data = new JSONObject();
            data.put("fishName", (Object) aiFishTypeEntity2.getFishName());
            data.put("fishType", (Object) aiFishTypeEntity2.getObject());
            datas2.add(data);
        });
        log.info("查询出口种类成功");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("data", (Object) datas2);
        return result;
    }

    @GetMapping("/getFishNameAndType")
    public JSONObject getFishNameAndType() {
        JSONObject result = new JSONObject();
        List<FishType> allFishType = fishTypeMapper.findAllType();
        if (CollectionUtils.isEmpty(allFishType)) {
            log.error("没有种类数据");
            result.put("status", false);
            result.put("data", null);
            return result;
        }

        log.info("查询种类成功");
        result.put("status", true);
        result.put("data", allFishType);
        return result;
    }
}